<template>
    <div>
        <div class="header-title">
            <span @click="$router.go(-1)" class="mint-toast-icon mintui mintui-back"></span>
            <span>注册</span>
            <span></span>
        </div>
        <div class="choose-type">
            <span @click="chooseType = 1" :class="[chooseType == 1?'chooseNow':'']">手机注册</span>
            <span @click="chooseType = 2" :class="[chooseType == 2?'chooseNow':'']">邮箱注册</span>
        </div>
        <!-- 手机注册 -->
        <div class="login-password" v-if="chooseType==1?true:false">
            <div class="input-area">
                <div @click="areaPage = true">
                    <span>手机</span>
                    <span>+{{phoneArea}}</span>
                    <span class="fa fa-angle-down"></span>
                </div>
                <input v-model="phoneRegister.accounts" type="text" placeholder="请输入手机号" />
            </div>
            <div class="write-line">
                <input v-model="phoneRegister.code" type="text" placeholder="请输入手机验证码" />
                <span @click="getCodeFather1">
                    <sendPhoneCode
                        :phoneArea="phoneArea"
                        :phone="phoneRegister.accounts"
                        ref="childCode"
                    ></sendPhoneCode>
                </span>
            </div>
            <div class="write-line">
                <input v-model="phoneRegister.password" type="password" placeholder="请输入密码" />
            </div>
            <div class="write-line">
                <input v-model="phoneRegister.passwordAgain" type="password" placeholder="请确认密码" />
            </div>
            <div class="write-line">
                <input v-model="phoneRegister.invitationCode" type="text" placeholder="请输入邀请码(选填)" />
            </div>
        </div>
        <!-- 邮箱注册 -->
        <div class="login-code login-password" v-if="chooseType==2?true:false">
            <div class="write-line">
                <input v-model="emailRegister.accounts" type="text" placeholder="请输入邮箱" />
            </div>
            <div class="write-line">
                <input v-model="emailRegister.code" type="text" placeholder="请输入邮箱验证码" />
                <span @click="getCodeFather2">
                    <sendEmailCode
                        :emailType="emailType"
                        :phone="emailRegister.accounts"
                        ref="childCode"
                    ></sendEmailCode>
                </span>
            </div>
            <div class="write-line">
                <input
                    v-model="emailRegister.password"
                    type="text"
                    placeholder="请输入8-16位数字和字母的组合密码"
                />
            </div>
            <div class="write-line">
                <input v-model="emailRegister.passwordAgain" type="text" placeholder="请确认密码" />
            </div>
            <div class="write-line">
                <input v-model="emailRegister.invitationCode" type="text" placeholder="请输入邀请码(选填)" />
            </div>
        </div>
        <!-- 注册状态 -->
        <div class="register-status">
            <div class="agree">
                <span
                    @click="agreePE =! agreePE"
                    :class="[agreePE==false?'':'agree-input-choose','agree-input', 'mui-icon', 'mui-icon-checkmarkempty']"
                ></span>
                <p>
                    我同意
                    <span>《用户协议》</span>
                </p>
            </div>
            <mt-button size="normal" @click="register" class="submit-btn">注册</mt-button>
            <p class="bottom-contact">注册异常，请联系我们</p>
            <p class="bottom-login">
                已有账号？
                <span @click="$router.push({name:'login'})">登录</span>
            </p>
        </div>
        <!-- 获取区号 -->
        <transition name="fade" mode="out-in">
            <div v-if="areaPage">
                <areaCode @getAreaPage="getAreaPage" @getPhoneArea="getPhoneArea"></areaCode>
            </div>
        </transition>
    </div>
</template>
<script>
import baseJs from "../../assets/js/base";
import sendEmailCode from "../../components/getEmailCode";
import sendPhoneCode from "../../components/getPhoneCode";
import areaCode from "../../components/areaCode";
import { Toast } from "mint-ui";
export default {
    data() {
        return {
            chooseType: "1", //注册类型的切换
            areaPage: false, //区号页的显示与隐藏
            agreePE: false, //是否统一协议
            phoneArea: "86", //手机区号的获取和更新
            emailType: "email", //确定获取验证码为邮箱类型
            //手机号注册
            phoneRegister: {
                accounts: "",
                password: "",
                passwordAgain: "",
                code: "",
                invitationCode: ""
            },
            //邮箱号注册
            emailRegister: {
                accounts: "",
                code: "",
                password: "",
                passwordAgain: "",
                invitationCode: ""
            }
        };
    },
    components: {
        sendPhoneCode,
        sendEmailCode,
        areaCode
    },
    methods: {
        register() {
            if (this.chooseType == 1) {
                var accountCategory = baseJs.checkPhoneNumOrEmailNum(
                    this.phoneRegister,
                    1,
                    1
                );
                console.log(accountCategory);
                if (accountCategory == "phone") {
                    var params = {
                        phoneNo: this.phoneRegister.accounts,
                        password: this.phoneRegister.password,
                        confirmPassword: this.phoneRegister.passwordAgain,
                        verificationCode: this.phoneRegister.code,
                        invitationCode: this.phoneRegister.invitationCode,
                        type: "0"
                    };
                    console.log(params);
                    this.registerPort(params);
                }
            } else if (this.chooseType == 2) {
                var accountCategory = baseJs.checkPhoneNumOrEmailNum(
                    this.emailRegister,
                    2,
                    1
                );
                console.log(accountCategory);
                if (accountCategory == "email") {
                    var params = {
                        mailbox: this.emailRegister.accounts,
                        password: this.emailRegister.password,
                        confirmPassword: this.emailRegister.passwordAgain,
                        verificationCode: this.emailRegister.code,
                        invitationCode: this.emailRegister.invitationCode,
                        type: "1"
                    };
                    console.log(params);
                    this.registerPort(params);
                }
            }
        },
        registerPort(params) {
            if (this.agreePE == false) {
                Toast({ message: "请先阅读并同意用户协议！" });
                return false;
            }
            this.$axios.post(this.address.register, params).then(res => {
                console.log(res.data);
                if (res.data.code == 200) {
                    Toast({ message: "登录成功" });
                    this.$store.commit(
                        "getUserMsg",
                        JSON.stringify(res.data.data)
                    );
                    this.$router.push({ name: "home" });
                } else {
                    Toast({ message: res.data.msg });
                }
            });
        },

        //获取手机验证码
        getCodeFather1() {
            this.$refs.childCode.getCodeChild(); 
        },
        getCodeFather2() {
            this.$refs.childCode.getCodeChild();
        },
        //关闭获取区号页
        getAreaPage(status) {
            this.areaPage = status;
        },
        //获取手机区号
        getPhoneArea(areaNum) {
            this.phoneArea = areaNum;
            console.log(this.phoneArea, 999);
        }
    }
};
</script>
<style lang="less" scoped>
.chooseNow {
    color: @blueColor5;
    border-bottom: 2px solid @blueColor10 !important;
}
.choose-type {
    .flexStartCenter();
    font-size: 14px;
    font-weight: bold;
    color: @blackColor5;
    padding: 0 15px;
    span {
        display: inline-block;
        width: 50%;
        text-align: center;
        padding: 10px 0;
        border-bottom: 2px solid @blackColor3;
    }
}

.login-password {
    padding: 15px;
    .write-line {
        padding: 20px 0;
        border-bottom: 1px solid @blackColor1;
        .flexBetweenCenter();
        > input {
            border: none;
            font-size: 13px;
            color: @blackColor5;
            width: 60%;
        }
        > img {
            width: 20px;
        }
        > span {
            display: inline-block;
            width: 30%;
            font-size: 15px;
            color: @blueColor5;
            text-align: center;
            font-weight: bold;
            border-left: 1px solid @blackColor1;
        }
    }
}

.register-status {
    padding: 15px;
    .submit-btn {
        margin: 30px 0;
    }
    .agree {
        .agree-input-choose {
            background-color: @blueColor5;
        }
        .flexStartCenter();
        .agree-input {
            display: inline-block;
            width: 20px;
            height: 20px;
            border: 1.5px solid @blueColor5;
            border-radius: 50%;
            color: white;
            .flexCenterCenter();
        }
        p {
            font-size: 13px;
            color: @blackColor5;
            margin-left: 10px;
            span {
                color: @blueColor5;
            }
        }
    }
    .bottom-contact {
        text-align: center;
        font-size: 13px;
        color: @blueColor5;
    }
    .bottom-login {
        font-size: 13px;
        color: @blackColor5;
        text-align: center;
        margin-top: 20px;
        > span {
            color: @blueColor5;
        }
    }
}
</style>
